package Servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

import Bean.User;
import Service.UserService;
import ServiceImpl.UserServiceImpl;

/**
 * Servlet implementation class registerServlet
 */
public class AddUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
    public AddUserServlet() {}

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html charset = UTF-8");
		UserService userservice = new UserServiceImpl();
		String username = request.getParameter("username");
		String passwd = request.getParameter("passwd");
		String name = request.getParameter("name");
		User user = new User();
		try{
			user.setUsername(username);
			user.setName(name);
			user.setPassword(passwd);
			if(userservice.adduser(user)) {
				System.out.println("yes");
			} else {
				System.out.println("no");
			}
			request.getRequestDispatcher("add_user.jsp").forward(request, response);
		}catch(Exception e) {
			request.getRequestDispatcher("add_user.jsp").forward(request, response);
		}
		
	}

}
